package com.fastloan.mgt.invest.utils;

import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import freemarker.template.TemplateExceptionHandler;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author rankai
 *         createTime 2017-08-2017/8/15 11:12
 */
public class DocUtil {

    public Configuration configure = null;

    public DocUtil() {

        configure = new Configuration(Configuration.VERSION_2_3_21);
        configure.setDefaultEncoding("utf-8");
    }

    /**
     * 根据Doc模板生成word文件
     *
     * @param dataMap      需要填入模板的数据
     * @param downloadType 文件名称
     * @param savePath     保存路径
     */
    public File createDoc(Map<String, Object> dataMap, String downloadType, String savePath) {

        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        String realPath = request.getServletContext().getRealPath("/WEB-INF/static/temp");
        Writer out = null;
        try {
            //加载需要装填的模板
            Template template;
            configure.setDirectoryForTemplateLoading(new File(realPath));
            configure.setTemplateExceptionHandler(TemplateExceptionHandler.IGNORE_HANDLER);
            //定义Template对象，注意模板类型名字与downloadType要一致
            template = configure.getTemplate(downloadType + ".ftl");
            File outFile = new File(savePath);
            out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile), "utf-8"));
            template.process(dataMap, out);
            return outFile;
        } catch (IOException e) {
            e.printStackTrace();
        } catch (TemplateException e) {
            e.printStackTrace();
        } finally {
            try {
                if (out != null) {
                    out.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

//    public String getImageStr(String imgFile) {
//        InputStream in = null;
//        byte[] data = null;
//        try {
//            in = new FileInputStream(imgFile);
//            data = new byte[in.available()];
//            in.read(data);
//            in.close();
//        } catch (FileNotFoundException e) {
//            e.printStackTrace();
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//        BASE64Encoder encoder = new BASE64Encoder();
//        return encoder.encode(data);
//    }

    public static void main(String[] args) {

        List<String> list = new ArrayList<>();
        list.add("1");
        list.add("2");
        list.add("3");
        DocUtil docUtil = new DocUtil();
        Map<String, Object> dataMap = new HashMap<String, Object>();
        dataMap.put("list", list);
        docUtil.createDoc(dataMap, "doc", "E:\\123.doc");
    }
}
